// 格式化时间
function formatTime(time){
  console.log('过滤器中的time',time)
  // "2019-08-07 10:53:00"
  let _time = time.replace(/-/g,"/")
  let publishTime = new Date(_time).getTime()
  let nowTime = new Date().getTime()
  
  // 不同时间的时间戳
  const twentyFourHours = 24 * 60 * 60 * 1000;
  const oneMin = 60 * 1000;
  const fiveteenMin = 15 * 60 * 1000;
  const thirtyMin = 30 * 60 * 1000;
  const oneHour = 60 * 60 * 1000;

  const date = new Date();
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const today = `${year}/${month}/${day}`;
  const todayTime = new Date(today).getTime();

  const yesterdayTime = new Date(todayTime - twentyFourHours).getTime();
  const lastYesterdayTime = new Date(todayTime - twentyFourHours*2).getTime();
  const lastThreeDays = new Date(todayTime - twentyFourHours*3).getTime();
  const lastFourDays = new Date(todayTime - twentyFourHours*4).getTime();
  const lastFiveDays = new Date(todayTime - twentyFourHours*5).getTime();
  const lastSixDays = new Date(todayTime - twentyFourHours*6).getTime();
  const lastSevenDays = new Date(todayTime - twentyFourHours*7).getTime();

  if (publishTime >= todayTime) {
    if(Math.floor(new Date(nowTime - publishTime) / (60 * 1000))==0) {
      return '刚刚'
    }
    else if (nowTime - publishTime < thirtyMin) {
      return Math.floor(new Date(nowTime - publishTime) / (60 * 1000)) + "分钟前"
    } else if (thirtyMin <= nowTime - publishTime && nowTime - publishTime < oneHour) {
      return "半小时前"
    } else {
      return Math.floor(new Date(nowTime - publishTime) / (60 * 60 * 1000)) + "小时前"
    }
  }
  else if (yesterdayTime <= publishTime && publishTime < todayTime) {
    return '昨天 ' + _time.split(' ')[1];
  }
  else if (lastYesterdayTime <= publishTime && publishTime < yesterdayTime) {
    return '前天 ' + _time.split(' ')[1];
  }
  else return time
  // else if (lastThreeDays <= publishTime && publishTime < lastYesterdayTime) {
  //   return '3天前';
  // }
  // else if (lastFourDays <= publishTime && publishTime < lastThreeDays) {
  //   return '4天前';
  // }
  // else if (lastFiveDays <= publishTime && publishTime < lastFourDays) {
  //   return '5天前';
  // }
  // else if (lastSixDays <= publishTime && publishTime < lastFiveDays) {
  //   return '6天前';
  // }
  // else if (lastSevenDays <= publishTime && publishTime < lastSixDays) {
  //   return '7天前';
  // }
  // else {
  //   return time.split(' ')[0];
  // }
}

//导出
export { formatTime };